#include "jpzhuji.h"

JpZhuji::JpZhuji()
{

}

JpZhuji::~JpZhuji()
{

}

void JpZhuji::run(){
    loadConfig();
    avgFenPei();
    int host_type=type_local();
    switch (host_type) {
    case 1:
    {
        //是主机
        MastSlaveMap hostinfo=getSelfMasterInfo();
        //showMasterInfo(hostinfo);
        JpMaster *jm=new JpMaster(hostinfo);
        jm->run();
      //  jm->wait();
        break;
    }
    case 2:{
        //是从机
        SlaveMastMap slaveinfo=getSelfSlaveInfo();
        //showSlaveInfo(slaveinfo);
        JpSlave *js=new JpSlave(slaveinfo);
        js->run();
        break;
    }
    case 3:
    {
        //是主机
        MastSlaveMap hostinfo=getSelfMasterInfo();
        //showMasterInfo(hostinfo);
        JpMaster *jm=new JpMaster(hostinfo);
        jm->run();
       // jm->wait();
        //是从机
        SlaveMastMap slaveinfo=getSelfSlaveInfo();
        //showSlaveInfo(slaveinfo);
        JpSlave *js=new JpSlave(slaveinfo);
        js->run();
        break;
    }
    default:
    {
        break;
    }

    }
}
//读取配置文件
void JpZhuji::loadConfig(){
    configmanage fm;
    masterlist=fm.getMasterIp();
    slavelist=fm.getSlaveIp();
    mastercount=fm.getMasterCount();
    slavecount=fm.getSlaveCount();
    discount=slavecount/mastercount;
}
//读取配置文件,平均分类主从机
void JpZhuji::avgFenPei(){

  int slaveindex=0;
  for(int i=0;i<mastercount;i++){
      MastSlaveMap item;
      item.masterip=masterlist.at(i);
      if(i!=mastercount-1){
         item.slavecount=discount;
         int tmps=slaveindex;
         for(int j=tmps;j<tmps+discount;j++){
            SlaveMastMap sitem;
            sitem.masterip=item.masterip;
            sitem.slaveip=slavelist[j];
            to_slave.append(sitem);
             item.slaveip.append(slavelist[j]);
            //slavelist.erase(j);
            slaveindex++;
         }
      }else{
          item.slavecount=(slavecount-slaveindex);
          for(int j=slaveindex;j<slavecount;j++){
              SlaveMastMap sitem;
              sitem.masterip=item.masterip;
              sitem.slaveip=slavelist[j];
              to_slave.append(sitem);
             item.slaveip.append(slavelist[j]);
          }
      }
      to_master.append(item);
  }


}
//判断自己是主机还是从机 1是主机 2是从机 3既是主机又是从机
int JpZhuji::type_local(){
    QString hostname=QHostInfo::localHostName();
    QHostInfo hinfo=QHostInfo::fromName(hostname);
    foreach (QHostAddress address, hinfo.addresses()) {
        int isexist=masterlist.indexOf(address.toString());
        int isexist2=slavelist.indexOf(address.toString());
        if(isexist2<0&&isexist>=0){
            return 1;
        }
        if(isexist2>=0&&isexist<0){
            return 2;
        }
        if(isexist2>=0&&isexist>=0){
            return 3;
        }

    }
    return 0;
}
//如果自己是主机,获得主机信息
MastSlaveMap JpZhuji::getSelfMasterInfo(){
    QString hostname=QHostInfo::localHostName();

    QHostInfo hinfo=QHostInfo::fromName(hostname);
    for(int i=0;i<to_master.length();i++){
        foreach (QHostAddress address, hinfo.addresses()) {
            if(to_master[i].masterip==address.toString()){
                return to_master[i];
            }

        }
    }
   MastSlaveMap mp;
   return mp;
}

//如果自己是从机,获得从机信息
SlaveMastMap JpZhuji::getSelfSlaveInfo(){
    QString hostname=QHostInfo::localHostName();

    QHostInfo hinfo=QHostInfo::fromName(hostname);
    for(int i=0;i<to_slave.length();i++){
        foreach (QHostAddress address, hinfo.addresses()) {
            if(to_slave[i].slaveip==address.toString()){
                return to_slave[i];
            }

        }
    }
   SlaveMastMap mp;
   return mp;
}



